package com.xiao.Uitils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class SqlSessionUitils {
    public  SqlSession SqlSession()  {
        //获取核心配置文件的字节输入流
        InputStream resourceAsStream = null;
        try {
            resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        //创建SqlSessionFactoryBuilder对象，然后再创建SqlSessionFactory对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        //获取SqlSession对象,并自动提交sqlSession事务
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        //获取SqlSession对象，sqlSession事务不会自动提交事务
        //SqlSession sqlSession = sqlSessionFactory.openSession();
        return sqlSession;
    }
}
